package cn.wetcland.urban.framework.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.wetcland.urban.common.MyAssert;
import cn.wetcland.urban.common.utils.CopyObjectUtil;
import cn.wetcland.urban.framework.dto.CustomerEvaluationDTO;
import cn.wetcland.urban.framework.mapper.UtEvaluationMapper;
import cn.wetcland.urban.framework.mapper.UtOrderMapper;
import cn.wetcland.urban.framework.service.UtEvaluationService;
import cn.wetcland.urban.generator.domain.UtEvaluation;
import cn.wetcland.urban.generator.domain.UtOrder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
* @author Wetcland
* @description 针对表【ut_evaluation】的数据库操作Service实现
* @createDate 2024-01-21 09:51:59
*/
@Service
public class UtEvaluationServiceImpl extends ServiceImpl<UtEvaluationMapper, UtEvaluation> implements UtEvaluationService {

    @Resource
    public UtEvaluationMapper evaluationMapper;

    @Resource
    public UtOrderMapper orderMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void handleCustomerEvaluation(CustomerEvaluationDTO evaluationDTO) {
        UtOrder utOrder = orderMapper.selectById(evaluationDTO.getOrderId());
        MyAssert.notNull(utOrder, "该订单不存在");
        UtEvaluation utEvaluation = new UtEvaluation();
        utEvaluation.setEvaluationCustomerId(StpUtil.getLoginIdAsLong());
        CopyObjectUtil.copyPropertiesIgnoreNull(evaluationDTO, utEvaluation);
        evaluationMapper.insert(utEvaluation);
    }

    @Override
    public UtEvaluation evaluationDetails(Long orderId) {
        return evaluationMapper.selectOne(new QueryWrapper<UtEvaluation>()
                .eq("order_id", orderId)
        );
    }
}




